//
//  GuestureLoginVC.m
//  Project
//
//  Created by 冯东辉 on 2018/9/30.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "GuestureLoginVC.h"
#import "DBGuestureLock.h"

@interface GuestureLoginVC ()

@property (nonatomic, strong) NSString *phoneNum;

@end

@implementation GuestureLoginVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setNavView];
    
    self.view.backgroundColor = Base_color;
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    _phoneNum = [defaults objectForKey:USER_PHONE_NUM];
    
    NSString *resultPassWord = [defaults objectForKey:[NSString stringWithFormat:@"%@_%@", DBGuestureLockPaswdResult, _phoneNum]];

    [defaults setValue:resultPassWord forKey:@"Me_i36_DBGuestureLock_Password"];
    
    DBGuestureLock *lock = [DBGuestureLock lockOnView:self.view onPasswordSet:^(DBGuestureLock *lock, NSString *password) {
        
    } onGetCorrectPswd:^(DBGuestureLock *lock, NSString *password) {
        
        // 密码验证成功 登录
        [self login];
        
    } onGetIncorrectPswd:^(DBGuestureLock *lock, NSString *password) {
        
        [SVProgressHUD showInfoWithStatus:@"密码错误，请重新输入！"];
        [SVProgressHUD dismissWithDelay:1.0f];
    }];
    
    [self.view addSubview:lock];
    
    UIButton *forgetBtn = [[UIButton alloc] initWithFrame:CGRectMake((GET_SCREEN_WIDTH()-100)/2.0f, GET_SCREEN_HEIGHT()-60, 100, 40)];
    [forgetBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [forgetBtn setTitleColor:UICOLOR_HEX(0xffffffff) forState:UIControlStateNormal];
    forgetBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [forgetBtn addTarget:self action:@selector(forgetGeusture) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetBtn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)forgetGeusture {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"FORGETGEUSTUREPASSWORD" object:nil];
}

#pragma mark - 自定义导航栏
- (void)setNavView {
    
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GET_SCREEN_WIDTH(), SafeAreaTopHeight)];
    navView.backgroundColor = Base_color;
    [self.view addSubview:navView];
    
    UILabel *navTitleL = [[UILabel alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight-44, GET_SCREEN_WIDTH(), 44)];
    navTitleL.text = @"手势密码登录";
    navTitleL.textColor = UICOLOR_HEX(0xffffffff);
    navTitleL.textAlignment = NSTextAlignmentCenter;
    navTitleL.font = [UIFont systemFontOfSize:16];
    [navView addSubview:navTitleL];
}

/**
 * 登录
 */
- (void)login {
    
    [SVProgressHUD showWithStatus:@"登录中..."];
    
    [[ProjectApi instance] loginWithUserId:_phoneNum callBack:^(NetModel *object) {
        
        if (object.isSuccessed) {
            
            NSDictionary *json_dic = [object.service firstObject];
            
            [self loginSuccessedHandle:json_dic];
            
        }else {
            
            switch (object.statusCode) {
                case 2:
                    [SVProgressHUD showErrorWithStatus:@"用户信息错误"];
                    [SVProgressHUD dismissWithDelay:1.5f];
                    break;
                case 3:
                    [SVProgressHUD showErrorWithStatus:@"无此用户信息"];
                    [SVProgressHUD dismissWithDelay:1.5f];
                    break;
                default:
                    [SVProgressHUD showErrorWithStatus:@"登录失败"];
                    [SVProgressHUD dismissWithDelay:1.5f];
                    break;
            }
        }
    }];
}

/**
 * 登录成功回调
 */
- (void)loginSuccessedHandle:(NSDictionary *)json_dic {
    
    [[NSUserDefaults standardUserDefaults] setObject:_phoneNum forKey:USER_PHONE_NUM];
    [[NSUserDefaults standardUserDefaults] setObject:json_dic forKey:USER_INFO_CITY];
    [[NSUserDefaults standardUserDefaults] synchronize];
    if ([_delegate respondsToSelector:@selector(LoginSuccessCallBack)]) {
        
        [_delegate LoginSuccessCallBack];
    }
}

@end
